package lero.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lero.pojo.Article;
import lero.vo.ArticleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController{

    //定义一个全局的日志记录器，通过LoggerFactory获取
    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    /**
     *功能描述:新增文章
     * @date 2018/7/16
     * @param article ：pojo实体
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/insert.do")
    @ResponseBody
    public HashMap insert(@ModelAttribute("article") Article article){
        articleServiceI.insert(article);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    @RequestMapping("/select.do")
    @ResponseBody
    public  PageInfo<Article> select(@RequestBody ArticleVo vo){
        PageHelper.startPage(vo.getCurrentPage(),vo.getRows());
        List<Article> articles = articleServiceI.select(vo);
        PageInfo<Article> info = new PageInfo<>(articles);
        return info;
    }

    /**
     *功能描述:删除单条数据
     * @date 2018/7/16
     * @param id
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public  HashMap delete(@RequestParam("id") int id){
        articleServiceI.delete(id);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }

    /**
     *功能描述:批量删除
     * @date 2018/7/16
     * @param ids  批量删除数据的id组成的数组
     * @return java.util.HashMap ：返回map数据
     */
    @RequestMapping("/deleteMany.do")
    @ResponseBody
    public  HashMap deleteMany(@RequestParam("ids[]") int[] ids){
        articleServiceI.deleteMany(ids);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


    /**
     *功能描述:
     * @date 2018/7/10
     * @param id : 查询文章的id
     * @return lero.pojo.Article
     */
    @RequestMapping("/selectOne.do")
    @ResponseBody
    public  Article selectOne(@RequestParam("id") int id){
        Article article = articleServiceI.selectOne(id);
        return article;
    }


    @RequestMapping(value="/update.do", method = RequestMethod.POST)
    @ResponseBody
    public  HashMap update(@ModelAttribute("article") Article article){
        articleServiceI.update(article);
        HashMap map = new HashMap();
        map.put("status",true);
        map.put("message",lero.common.Constant.SUCCESS.toString());
        return map;
    }


}
